﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace YArchitech.Temp
{
	public class HYPipeline : HYMEPObject, IHYXmlSerial
	{
		public IHYSpecification Section
		{
			get
			{
				return this.m_Section;
			}
			set
			{
				this.m_Section = value;
			}
		}

		public new double Length { get; set; }

		public new double Velocity
		{
			get
			{
				return this.m_Velocity;
			}
			set
			{
				this.m_Velocity = value;
			}
		}

		public double Re { get; set; }

		public double DynamicPressure
		{
			get
			{
				return this.m_DynamicPressure;
			}
		}

		public double Coefficient
		{
			get
			{
				return this.m_Coefficient;
			}
		}

		public double Lambda
		{
			get
			{
				return this.m_Lambda;
			}
		}

		public double Rm
		{
			get
			{
				return this.m_Rm;
			}
			set
			{
				this.m_Rm = value;
			}
		}

		public double ShockResistance
		{
			get
			{
				return this.m_ShockResistance;
			}
			set
			{
				this.m_ShockResistance = value;
			}
		}

		public double FrictionalResistance
		{
			get
			{
				return this.m_FrictionalResistance;
			}
			set
			{
				this.m_FrictionalResistance = value;
			}
		}

		public double Resistance
		{
			get
			{
				return this.m_Resistance;
			}
			set
			{
				this.m_Resistance = value;
			}
		}

		public List<HYPipeline.AttachObject> AttachObjects
		{
			get
			{
				return this.m_AttachedObjects;
			}
		}

		public override void SetNext(HYMEPObject nextObj)
		{
			base.SetNext(nextObj);
		}

		public override void RebuildIndex(ref int index)
		{
			base.Index = index;
			index++;
			if (base.Next != null)
			{
				base.Next.RebuildIndex(ref index);
			}
		}

		public virtual void AddAttachedObject(HYMEPObject obj, string showName, double val, bool isTerminal = false, int PartIndex = 0)
		{
			if (this.m_AttachedObjects == null)
			{
				this.m_AttachedObjects = new List<HYPipeline.AttachObject>();
			}
			HYPipeline.AttachObject attachObject = this.m_AttachedObjects.Find((HYPipeline.AttachObject ao) => ao.Object.Id == obj.Id);
			if (attachObject == null)
			{
				attachObject = new HYPipeline.AttachObject
				{
					Object = obj,
					ShowName = showName,
					Value = val,
					IsTerminal = isTerminal
				};
				this.m_AttachedObjects.Add(attachObject);
				return;
			}
			attachObject.ShowName = showName;
			attachObject.Value = val;
			attachObject.IsTerminal = isTerminal;
			attachObject.PartIndex = PartIndex;
		}

		public void GetAttachObjectInfo(out double coefficient, out double equipmentResistance)
		{
			coefficient = 0.0;
			equipmentResistance = 0.0;
			if (this.m_AttachedObjects == null || this.m_AttachedObjects.Count == 0)
			{
				return;
			}
			foreach (HYPipeline.AttachObject attachObject in this.m_AttachedObjects)
			{
				if (attachObject.IsTerminal)
				{
					equipmentResistance += attachObject.Value;
				}
				else if (attachObject.Object is HYAccessory)
				{
					equipmentResistance += attachObject.Value;
				}
				else
				{
					coefficient += attachObject.Value;
				}
			}
			this.m_Coefficient = coefficient;
			this.m_ShockResistance = equipmentResistance;
		}

		public void CalVelocity()
		{
			this.m_Velocity = this.m_Section.GetVelocity(base.Flow);
		}

		public virtual double CalLambda(HYSystemOption option)
		{
			double d = this.m_Section.D;
			this.Re = Hydrokinetics.Re(d, this.m_Velocity, option.Viscosity);
			double kd = option.Roughness / d;
			this.m_Lambda = Hydrokinetics.x_TF_CW(this.Re, kd);
			return this.m_Lambda;
		}

		public virtual double CalRm(HYSystemOption option)
		{
			this.m_Lambda = this.CalLambda(option);
			this.m_Rm = Hydrokinetics.Rm(this.m_Lambda, this.m_Section.Rs, this.m_Velocity, option.Density);
			return this.m_Rm;
		}

		public override void Cal(HYSystemOption option)
		{
			if (this.m_Section == null)
			{
				throw new Exception("Section not set!");
			}
			this.CalVelocity();
			this.m_DynamicPressure = Hydrokinetics.DynamicPressure(this.m_Velocity, option.Density);
			this.m_Rm = this.CalRm(option);
			this.m_Rm *= option.RoughnessFix;
			this.m_Rm *= option.FrictionFix;
			this.m_FrictionalResistance = this.m_Rm * this.Length;
			double num = 0.0;
			double num2 = 0.0;
			this.GetAttachObjectInfo(out num, out num2);
			this.m_ShockResistance = this.m_Coefficient * this.m_DynamicPressure * option.ShockFix + num2;
			this.m_Resistance = this.m_FrictionalResistance + this.m_ShockResistance;
		}

		public override void WriteXml(XElement xe)
		{
			base.WriteXml(xe);
			xe.SetAttributeValue("Flow", base.Flow);
			xe.SetAttributeValue("Length", this.Length);
			xe.SetAttributeValue("Velocity", this.m_Velocity);
			xe.SetAttributeValue("DynamicPressure", this.m_DynamicPressure);
			xe.SetAttributeValue("Rm", this.m_Rm);
			xe.SetAttributeValue("Coefficient", this.m_Coefficient);
			xe.SetAttributeValue("ShockResistance", this.m_ShockResistance);
			xe.SetAttributeValue("FrictionalResistance", this.m_FrictionalResistance);
			xe.SetAttributeValue("Resistance", this.m_Resistance);
			XElement xe2 = xe.ChildElement("Section");
			this.m_Section.WriteXml(xe2);
		}

		public override void ReadXml(XElement xe)
		{
			base.ReadXml(xe);
			base.Flow = xe.GetDoubleAttribute("Flow", 0.0);
			this.Length = xe.GetDoubleAttribute("Length", 0.0);
			this.m_Velocity = xe.GetDoubleAttribute("Velocity", 0.0);
			this.m_DynamicPressure = xe.GetDoubleAttribute("DynamicPressure", 0.0);
			this.m_Rm = xe.GetDoubleAttribute("Rm", 0.0);
			this.m_ShockResistance = xe.GetDoubleAttribute("ShockResistance", 0.0);
			this.m_FrictionalResistance = xe.GetDoubleAttribute("FrictionalResistance", 0.0);
			this.m_Resistance = xe.GetDoubleAttribute("Resistance", 0.0);
			XElement xe2 = xe.ChildElement("Section");
			this.m_Section.ReadXml(xe2);
		}

		protected List<HYPipeline.AttachObject> m_AttachedObjects;

		protected double m_DynamicPressure;

		protected double m_Coefficient;

		protected double m_Rm;

		protected double m_Lambda;

		protected double m_ShockResistance;

		protected double m_FrictionalResistance;

		protected double m_Resistance;

		protected double m_Velocity;

		protected IHYSpecification m_Section;

		public class AttachObject
		{
			public HYMEPObject Object { get; set; }

			public string ShowName { get; set; }

			public double Value { get; set; }

			public bool IsTerminal { get; set; }

			public int PartIndex { get; set; }

			public void Save()
			{
				if (this.Object is HYTerminal)
				{
					(this.Object as HYTerminal).Resistance = this.Value;
					return;
				}
				if (this.Object is HYAccessory)
				{
					(this.Object as HYAccessory).ShockResistance = this.Value;
					return;
				}
				if (this.Object is HYFitting)
				{
					(this.Object as HYFitting).SetCoeffient(this.Value, this.PartIndex);
				}
			}
		}
	}
}
